﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Zhaoxi.AuthenticationCenter.Utility;

namespace Zhaoxi.AuthenticationCenter.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class JWTController : ControllerBase
    {
        private IJWTService _IJWTService = null;
        public JWTController(IJWTService iJWTService)
        {
            this._IJWTService = iJWTService;
        }

        [HttpPost]
        public IActionResult GetToken(string name, string password)
        {
            Console.WriteLine($"{name}--{password}");

            if ("eleven".Equals(name) & "123456".Equals(password))
            {
                //生成JWT 
                string token = _IJWTService.GetToken(name);
                return new JsonResult(new
                {
                    Result = true,
                    Token = token
                });
            }
            else
            {
                return new JsonResult(new
                {
                    Result = false,
                    Token = " "
                });
            }

        }


        [HttpGet]
        [Route("/JWT/Info")]
        public IActionResult Info()
        {
            return new JsonResult(new
            {
                Result = true,
                Message = "This is Info OK!"
            });
        }


        [HttpGet]
        [Route("/JWT/InfoWithAuth")]
        //[Authorize(Roles = "Admin")]
        //[Authorize(Policy = "AdminPolicy")]//支持自定义 等价于[Authorize(Roles = "Admin")]
        //[Authorize(Policy = "AssertionAdminPolicy")]//支持自定义 等价于[Authorize(Roles = "Admin")]
        [Authorize(Policy = "CompulexPolicy")]
        public IActionResult InfoWithAuth()
        {
            return new JsonResult(new
            {
                Result = true,
                Message = "This is InfoWithAuth OK!"
            });
        }
    }
}
